package com.vue.vuedemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vue.vuedemo.annotation.CheckToken;
import com.vue.vuedemo.map.GoodsMapper;
import com.vue.vuedemo.pojo.Comment;
import com.vue.vuedemo.pojo.Goods;
import com.vue.vuedemo.util.ListUtils;
import com.vue.vuedemo.viewpojo.ResList;
import com.vue.vuedemo.viewpojo.ResObject;
import com.vue.vuedemo.vo.CommentVo;
import com.vue.vuedemo.vo.GoodsVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class GoodsController {
    @Autowired
    GoodsMapper goodsMapper;

    @CheckToken
    @GetMapping("/getgoods")
    public ResList<Goods> getgoods(HttpServletResponse response,int pageindex){
        response.setHeader("Access-Control-Allow-Origin", "*");
        PageHelper.startPage(pageindex,10,"id");
        List<Goods> allGoods = goodsMapper.getAllGoods();
        PageInfo<Goods> page = new PageInfo<>(allGoods);
        
        ResList<Goods> resList = new ResList();
        resList.setStatus(0);
        resList.setMessage(page.getList());
        
        
        return resList;
    }

    @CheckToken
    @GetMapping("/goods/getinfo/{id}")
    public ResList<Goods> getGoodsInfo(HttpServletResponse response, @PathVariable("id")Integer id){
        response.setHeader("Access-Control-Allow-Origin", "*");
        Goods goodsInfo = goodsMapper.getGoodsInfo(id);
        
        
        ResList<Goods> goodsRes = new ResList<Goods>();
        goodsRes.setStatus(0);
        goodsRes.setMessage(Arrays.asList(goodsInfo));
        
        
        return goodsRes;
    }

    @CheckToken
    @GetMapping("/goods/getdesc/{id}")
    public ResList<Goods> getdesc(HttpServletResponse response, @PathVariable("id")Integer id){
        response.setHeader("Access-Control-Allow-Origin", "*");
        Goods desc = goodsMapper.getDesc(id);
        
        ResList<Goods> goodsRes = new ResList<Goods>();
        goodsRes.setStatus(0);
        goodsRes.setMessage(Arrays.asList(desc));
        
        return goodsRes;
    }

    @CheckToken
    @GetMapping("/goods/getshopcarlist/{ids}")
    public ResList<GoodsVo> getshopcarlist(@PathVariable String ids,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String[] split = ids.split(",");
        List<Goods> goodShop=new ArrayList<>();
        for (String s : split) {
            Goods shopcart = goodsMapper.getShopcart(Integer.parseInt(s));
            goodShop.add(shopcart);
        }
        ListUtils<GoodsVo> utils = BeanUtils.instantiate(ListUtils.class);
        List<GoodsVo> userInfoListTarget = new ArrayList();
        utils.copyList(goodShop, userInfoListTarget, GoodsVo.class);
          
        
        ResList<GoodsVo> goodsList = new ResList<GoodsVo>();
        goodsList.setStatus(0);
        goodsList.setMessage(userInfoListTarget);
        
        
        return goodsList;
    }
}
